<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/14
 * Time: 18:21
 */

namespace app\index\model;

use think\Db;
use think\Exception;

class Order
{
    /*
     * 当日新订单，因时间间隔原因，需对比
     *
     * */
    public function getNewOrder()
    {
        return DB::query("SELECT
                of.id
            FROM
                (
                    SELECT
                        orderId id
                    FROM
                        homegarden.order_from
                    WHERE
                        TO_DAYS(transactionTime) = TO_DAYS(CURDATE())
                    AND `status` IN (1, 3, 4, 5)
                ) of
            LEFT JOIN (
                SELECT
                    id
                FROM
                    homegarden_oa.order_info
                WHERE
                    TO_DAYS(`time`) = TO_DAYS(CURDATE())
            ) oi ON oi.id = of.id
            WHERE
                oi.id IS NULL");
    }

    public function getRefundOrder()
    {
        return DB::query("SELECT
                of.id
            FROM
                (
                    SELECT
                        orderId id
                    FROM
                        homegarden.order_from
                    WHERE
                        TO_DAYS(transactionTime) = TO_DAYS(CURDATE())
                    AND `status` IN (4, 5)
                ) of
            LEFT JOIN (
                SELECT
                    id
                FROM
                    homegarden_oa.order_info
                WHERE
                    TO_DAYS(`time`) = TO_DAYS(CURDATE())
                AND `status` = 0
            ) oi ON oi.id = of.id
            WHERE
                oi.id IS NULL");
    }

    public function getOrderArr($id_arr)
    {
        $info = $this->getOrderInfo($id_arr);
        $detail = $this->getOrderDetail($id_arr);
        $bill = $this->getOrderBill($id_arr);
        return ['info' => $info, 'detail' => $detail, 'bill' => $bill];
    }

    public function addOrder($ord_arr)
    {
        try {
            DB::table('homegarden_oa.order_info')->insertAll($ord_arr['info']);
            DB::table('homegarden_oa.order_detail')->insertAll($ord_arr['detail']);
            DB::table('homegarden_oa.order_bill')->insertAll($ord_arr['bill']);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function getOrderInfo($id_arr)
    {
        $map['of.orderId'] = ['in', $id_arr];
        return DB::table('homegarden.order_from')->alias('of')
            ->join('homegarden.order_address oa', 'oa.id = of.orderId')
            ->join('homegarden.vegetable_lng_lat vll', 'vll.id = oa.eid')->where($map)
            ->column("
            of.orderId id,
            CASE of.status WHEN 4 THEN 0 WHEN 5 THEN 0 ELSE of.status END status,
            oa.eid site_id,
            vll.entityaddressId site_code,
            of.transactionTime time,
            of.userId user_id,
            oa.relation_name user_name,
            oa.relation_phone user_phone,
            oa.address_type delivery_type,
            left(of.distributionTime, 10) delivery_date,
            oa.address_des delivery_address,
            of.remark
        ");
    }

    public function getOrderDetail($id_arr)
    {
        $map['orderId'] = ['in', $id_arr];
        return DB::table('homegarden.order_detail')->where($map)->column("
            orderId order_id,
            pid product_id,
            productId product_code,
            productAmout number
        ");
    }

    public function getOrderBill($id_arr)
    {
        $map['orderId'] = ['in', $id_arr];
        return DB::table('homegarden.order_from')->alias('of')
        ->join('homegarden.coupon_detail cd', 'cd.coupon_sn = of.coupponSn', 'LEFT')
        ->where($map)->column("
            of.orderId order_id,
            of.productPrice pay,
            of.activePrice active,
            of.couponMoney coupon,
            cd.coupon_id,
            of.coupponSn coupon_sn,
            of.vegetableMoney integral,
            of.balanceMoney balance,
            of.freight,
            CASE of.relityPrice WHEN 0 THEN 0 ELSE of.payType END pay_type,
            of.relityPrice paid,
            of.modificationTime time,
            of.transactionNumber transaction_number
        ");
    }

    public function refundOrder($id_arr)
    {
        $refund = $this->getOrderRefund($id_arr);
        try {
            DB::table('homegarden_oa.order_refund')->insertAll($refund);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function getOrderRefund($id_arr)
    {
        $map['orderId'] = ['in', $id_arr];
        return DB::table('homegarden.order_from')->where($map)->column("
            orderId order_id,
            refundTime time,
            transactionNumber transaction_number,
            payType pay_type,
            relityPrice paid,
            vegetableMoney integral,
            balanceMoney balance,
            freight,
            CASE status WHEN 4 THEN 0 WHEN 5 THEN 1 END status
        ");
    }
}